home *** CD-ROM | disk | FTP | other *** search
/ Dr. Windows 3 / dr win3.zip / dr win3 / PROGRAMR / OLE2BOOK.ZIP / CHAP06.ZIP / CHAP06 / COSCHMOO / COSCHMOO.CPP < prev    next >
C/C++ Source or Header  |  1993-06-15  |  14KB  |  610 lines

  1. /*
  2.  * COSCHMOO.CPP
  3.  * Component Schmoo Chapter 6
  4.  *
  5.  * WinMain and CSchmooFrame implementations.
  6.  *
  7.  * Copyright (c)1993 Microsoft Corporation, All Rights Reserved
  8.  *
  9.  * Kraig Brockschmidt, Software Design Engineer
  10.  * Microsoft Systems Developer Relations
  11.  *
  12.  * Internet  :  kraigb@microsoft.com
  13.  * Compuserve:  >INTERNET:kraigb@microsoft.com
  14.  */
  15.  
  16.  
  17.  
  18. #define INITGUIDS
  19. #include "coschmoo.h"
  20.  
  21.  
  22. /*
  23.  * WinMain
  24.  *
  25.  * Purpose:
  26.  *  Main entry point of application.   Should register the app class
  27.  *  if a previous instance has not done so and do any other one-time
  28.  *  initializations.
  29.  */
  30.  
  31. int PASCAL WinMain (HINSTANCE hInst, HINSTANCE hPrev, LPSTR pszCmdLine, int nCmdShow)
  32.     {
  33.     LPCSchmooFrame  pFR;
  34.     FRAMEINIT       fi;
  35.     WPARAM          wRet;
  36.  
  37.    #ifdef WIN32
  38.     SetMessageQueue(96);
  39.    #endif
  40.  
  41.     //Attempt to allocate and initialize the application
  42.     pFR=new CSchmooFrame(hInst, hPrev, pszCmdLine, nCmdShow);
  43.  
  44.     fi.idsMin=IDS_FRAMEMIN;
  45.     fi.idsMax=IDS_FRAMEMAX;
  46.     fi.idsStatMin=IDS_STATMESSAGEMIN;
  47.     fi.idsStatMax=IDS_STATMESSAGEMAX;
  48.     fi.idStatMenuMin=ID_MENUFILE;
  49.     fi.idStatMenuMax=ID_MENUHELP;
  50.     fi.iPosWindowMenu=WINDOW_MENU;
  51.     fi.cMenus=CMENUS;
  52.  
  53.     //If we can initialize pFR, start chugging messages
  54.     if (pFR->FInit(&fi))
  55.         wRet=pFR->MessageLoop();
  56.  
  57.     delete pFR;
  58.     return wRet;
  59.     }
  60.  
  61.  
  62.  
  63.  
  64. /*
  65.  * CSchmooFrame::CSchmooFrame
  66.  * CSchmooFrame::~CSchmooFrame
  67.  *
  68.  * Constructor Parameters:
  69.  *  hInst           HINSTANCE from WinMain
  70.  *  hInstPrev       HINSTANCE from WinMain
  71.  *  pszCmdLine      LPSTR from WinMain
  72.  *  nCmdShow        int from WInMain
  73.  */
  74.  
  75. CSchmooFrame::CSchmooFrame(HINSTANCE hInst, HINSTANCE hInstPrev
  76.     , LPSTR pszCmdLine, int nCmdShow)
  77.     : CFrame(hInst, hInstPrev, pszCmdLine, nCmdShow)
  78.     {
  79.     UINT        i;
  80.  
  81.     for (i=0; i<5; i++)
  82.         m_hBmpLines[i]=NULL;
  83.  
  84.     m_fInitialized=FALSE;
  85.     return;
  86.     }
  87.  
  88.  
  89. CSchmooFrame::~CSchmooFrame(void)
  90.     {
  91.     UINT        i;
  92.  
  93.     for (i=0; i<5; i++)
  94.         {
  95.         if (NULL!=m_hBmpLines[i])
  96.             DeleteObject(m_hBmpLines[i]);
  97.         }
  98.  
  99.     if (m_fInitialized)
  100.         CoUninitialize();
  101.  
  102.     return;
  103.     }
  104.  
  105.  
  106.  
  107.  
  108. /*
  109.  * CSchmooFrame::FInit
  110.  *
  111.  * Purpose:
  112.  *  Call CoInitialize then calling down into the base class
  113.  *  initialization.
  114.  *
  115.  * Parameters:
  116.  *  pFI             LPFRAMEINIT containing initialization parameters.
  117.  *
  118.  * Return Value:
  119.  *  BOOL            TRUE if initialization succeeded, FALSE otherwise.
  120.  */
  121.  
  122. BOOL CSchmooFrame::FInit(LPFRAMEINIT pFI)
  123.     {
  124.     DWORD       dwVer;
  125.  
  126.     dwVer=CoBuildVersion();
  127.  
  128.     if (rmm!=HIWORD(dwVer))
  129.         return FALSE;
  130.  
  131.     if (FAILED(CoInitialize(NULL)))
  132.         return FALSE;
  133.  
  134.     m_fInitialized=TRUE;
  135.  
  136.     return CFrame::FInit(pFI);
  137.     }
  138.  
  139.  
  140.  
  141.  
  142.  
  143. /*
  144.  * CSchmooFrame::CreateCClient
  145.  *
  146.  * Purpose:
  147.  *  Constructs a new client specific to the application.
  148.  *
  149.  * Parameters:
  150.  *  None
  151.  *
  152.  * Return Value:
  153.  *  LPCClient       Pointer to the new client object.
  154.  */
  155.  
  156. LPCClient CSchmooFrame::CreateCClient(void)
  157.     {
  158.     return (LPCClient)(new CSchmooClient(m_hInst));
  159.     }
  160.  
  161.  
  162.  
  163.  
  164.  
  165.  
  166. /*
  167.  * CSchmooFrame::FPreShowInit
  168.  *
  169.  * Purpose:
  170.  *  Called from FInit before intially showing the window.  We do whatever
  171.  *  else we want here, modifying nCmdShow as necessary which affects
  172.  *  ShowWindow in FInit.
  173.  *
  174.  * Parameters:
  175.  *  None
  176.  *
  177.  * Return Value:
  178.  *  BOOL            TRUE if this initialization succeeded, FALSE otherwise.
  179.  */
  180.  
  181. BOOL CSchmooFrame::FPreShowInit(void)
  182.     {
  183.     CreateLineMenu();
  184.     CheckLineSelection(IDM_LINESOLID);
  185.     m_pGB->Check(IDM_LINESOLID, TRUE);
  186.     return TRUE;
  187.     }
  188.  
  189.  
  190.  
  191.  
  192.  
  193.  
  194. /*
  195.  * CSchmooFrame::CreateLineMenu
  196.  *
  197.  * Purpose:
  198.  *  Initializes the bitmaps used to create the Line menu and replaces
  199.  *  the text items defined in the application resources with these
  200.  *  bitmaps.  Note that the contents of m_hBmpLines must be cleaned
  201.  *  up when the application terminates.
  202.  *
  203.  * Parameters:
  204.  *  None
  205.  *
  206.  * Return Value:
  207.  *  None
  208.  */
  209.  
  210. void CSchmooFrame::CreateLineMenu(void)
  211.     {
  212.     HMENU       hMenu;
  213.     HDC         hDC, hMemDC;
  214.     HPEN        hPen;
  215.     HGDIOBJ     hObj;
  216.     TEXTMETRIC  tm;
  217.     UINT        i, cx, cy;
  218.  
  219.  
  220.     hMenu=GetSubMenu(GetMenu(m_hWnd), 3);   //Line menu.
  221.     hDC=GetDC(m_hWnd);
  222.  
  223.     //Create each line in a menu item 8 chars wide, one char high.
  224.     GetTextMetrics(hDC, &tm);
  225.     cx=tm.tmAveCharWidth*8;
  226.     cy=tm.tmHeight;
  227.  
  228.     //Create a memory DC in which to draw lines, and bitmaps for each line.
  229.     hMemDC=CreateCompatibleDC(hDC);
  230.     ReleaseDC(m_hWnd, hDC);
  231.  
  232.     for (i=0; i<5; i++)
  233.         {
  234.         m_hBmpLines[i]=CreateCompatibleBitmap(hMemDC, cx, cy);
  235.         SelectObject(hMemDC, m_hBmpLines[i]);
  236.  
  237.         PatBlt(hMemDC, 0, 0, cx, cy, WHITENESS);
  238.  
  239.         hPen=CreatePen(i, 1, 0L);           //i==line style like PS_SOLID
  240.         hObj=SelectObject(hMemDC, hPen);
  241.  
  242.         MoveTo(hMemDC, 0,  cy/2);
  243.         LineTo(hMemDC, cx, cy/2);
  244.  
  245.         ModifyMenu(hMenu, IDM_LINEMIN+i, MF_BYCOMMAND | MF_BITMAP
  246.             , IDM_LINEMIN+i, (LPCSTR)MAKELONG(m_hBmpLines[i], 0));
  247.  
  248.         SelectObject(hMemDC, hObj);
  249.         DeleteObject(hPen);
  250.         }
  251.  
  252.     CheckMenuItem(hMenu, IDM_LINESOLID, MF_CHECKED);
  253.     DeleteDC(hMemDC);
  254.  
  255.     return;
  256.     }
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266. /*
  267.  * CSchmooFrame::CreateGizmos
  268.  *
  269.  * Purpose:
  270.  *  Procedure to create all the necessary gizmobar buttons.
  271.  *
  272.  * Parameters:
  273.  *  None
  274.  *
  275.  * Return Value:
  276.  *  UINT            Number of gizmos added to the bar.
  277.  */
  278.  
  279. UINT CSchmooFrame::CreateGizmos(void)
  280.     {
  281.     UINT            iLast;
  282.     UINT            uState=GIZMO_NORMAL;
  283.     UINT            utCmd =GIZMOTYPE_BUTTONCOMMAND;
  284.     UINT            utEx  =GIZMOTYPE_BUTTONATTRIBUTEEX;
  285.  
  286.     //Insert the standard ones.
  287.     iLast=CFrame::CreateGizmos();
  288.  
  289.     //Insert File Import in the 5th position and account for it in iLast.
  290.     m_pGB->Add(utCmd, 4, IDM_FILEIMPORT, m_dxB, m_dyB, NULL, m_hBmp, 2, uState);
  291.     iLast++;
  292.  
  293.     //Separator
  294.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  295.  
  296.     //For the Background bitmap, preserve our use of black (part of the image)
  297.     m_pGB->Add(utCmd, iLast++, IDM_COLORBACKGROUND, m_dxB, m_dyB, NULL, m_hBmp, 3
  298.                , GIZMO_NORMAL | PRESERVE_BLACK);
  299.  
  300.     m_pGB->Add(utCmd, iLast++, IDM_COLORLINE, m_dxB, m_dyB, NULL, m_hBmp, 4, uState);
  301.  
  302.     //Separator
  303.     m_pGB->Add(GIZMOTYPE_SEPARATOR, iLast++, 0, 6, m_dyB, NULL, NULL, 0, uState);
  304.  
  305.     //Line styles.
  306.     m_pGB->Add(utEx, iLast++, IDM_LINESOLID,      m_dxB, m_dyB, NULL, m_hBmp, 5, uState);
  307.     m_pGB->Add(utEx, iLast++, IDM_LINEDASH,       m_dxB, m_dyB, NULL, m_hBmp, 6, uState);
  308.     m_pGB->Add(utEx, iLast++, IDM_LINEDOT,        m_dxB, m_dyB, NULL, m_hBmp, 7, uState);
  309.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOT,    m_dxB, m_dyB, NULL, m_hBmp, 8, uState);
  310.     m_pGB->Add(utEx, iLast++, IDM_LINEDASHDOTDOT, m_dxB, m_dyB, NULL, m_hBmp, 9, uState);
  311.  
  312.     return iLast;
  313.     }
  314.  
  315.  
  316.  
  317.  
  318.  
  319.  
  320.  
  321.  
  322. /*
  323.  * CSchmooFrame::OnCommand
  324.  *
  325.  * Purpose:
  326.  *  WM_COMMAND handler for the Schmoo frame window that just processes
  327.  *  the line menu and the color menu leaving the CFrame to do everything
  328.  *  else.
  329.  *
  330.  * Parameters:
  331.  *  hWnd            HWND of the frame window.
  332.  *  wParam          WPARAM of the message.
  333.  *  lParam          LPARAM of the message.
  334.  *
  335.  * Return Value:
  336.  *  LRESULT         Return value for the message.
  337.  */
  338.  
  339. LRESULT CSchmooFrame::OnCommand(HWND hWnd, WPARAM wParam, LPARAM lParam)
  340.     {
  341.     LPCSchmooDoc    pDoc;
  342.     char            szFile[CCHPATHMAX];
  343.     BOOL            fOK;
  344.     UINT            i, uTemp;
  345.     COLORREF        rgColors[16];
  346.     CHOOSECOLOR     cc;
  347.  
  348.     COMMANDPARAMS(wID, wCode, hWndMsg);
  349.  
  350.     /*
  351.      * Don't bother with anything during first initialization,
  352.      * skipping many GizmoBar notifications.
  353.      */
  354.     if (m_fInit)
  355.         return 0L;
  356.  
  357.     pDoc=(LPCSchmooDoc)m_pCL->ActiveDocument();
  358.  
  359.     /*
  360.      * Check for the line style commands which are ID